package com.cooper.service.admin;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cooper.constants.DictConstants;
import com.cooper.dao.admin.MallCategoryDAO;
import com.cooper.entity.MallCategoryEntity;
import com.cooper.resource.IResourceManager;
import com.cooper.util.BeanUtils;
import com.cooper.vo.MallCategoryVo;
import com.google.common.collect.Lists;

@Service
public class MallCategoryService {
	
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
	public static final long ROOT_CATEGORY_ID = 1L;
	
	@Autowired
	private IResourceManager resourceManager;
	
	@Autowired
	MallCategoryDAO mallCategoryDAO;
	
	@SuppressWarnings("unchecked")
	public List<MallCategoryEntity> getChildCategoryList(Long categoryId){
		return (List<MallCategoryEntity>)this.resourceManager.getResListByGroup(
				DictConstants.MALL_CATEGORY, "parent_id", String.valueOf(categoryId));
	}
	
	public List<MallCategoryVo> getAllCategoryWithChilds(){
		List<MallCategoryVo> retList = Lists.newArrayList();
//		List<MallCategoryEntity> entityList = this.getChildCategoryList(ROOT_CATEGORY_ID);
//		for(MallCategoryEntity entity: entityList){
//			MallCategoryVo categoryVo = new MallCategoryVo();
//			BeanUtils.copyProperties(entity, categoryVo);
//			retList.add(this.getCategoryVoWithChilds(categoryVo));
//		}
		return retList;
	}
	
	private MallCategoryVo getCategoryVoWithChilds(MallCategoryVo vo){
		if(1 == vo.getHasChild()){
			List<MallCategoryEntity> childList = this.getChildCategoryList(vo.getId());
			if(CollectionUtils.isNotEmpty(childList)){
				for(MallCategoryEntity child: childList){
					MallCategoryVo childVo = new MallCategoryVo();
					BeanUtils.copyProperties(child, childVo);
					childVo = this.getCategoryVoWithChilds(childVo);
					vo.addChild(childVo);
				}
			}
		}
		return vo;
	}
	
	public long saveMallCategory(MallCategoryVo vo){
		try{
			MallCategoryEntity entity = new MallCategoryEntity();
			boolean isUpdate = false;
			boolean isChangeOrgName = false;
			String oldFullNamePath = null;
			if(vo.getId()!=null){
				isUpdate = true;
				entity = mallCategoryDAO.getMallCategory(vo.getId());
				if(!vo.getName().equals(entity.getName())){
					isChangeOrgName = true;
					oldFullNamePath = entity.getFullNamePath();
					vo.setFullNamePath(entity.getFullNamePath().substring(0, entity.getFullNamePath().lastIndexOf("/") + 1) + vo.getName());
				}
			}
			BeanUtils.assembleUpdatePropertiesForEntity(vo, entity);
			if(isUpdate){
				mallCategoryDAO.updateMallCategory(entity);
				if(isChangeOrgName && 1 == entity.getHasChild()){//修改组织名称，需要更新子节点全路径
					this.mallCategoryDAO.updateChildFullPathWhenChangeName(entity, oldFullNamePath);
				}
			}else{
				long id = mallCategoryDAO.addMallCategory(entity);
				vo.setId(id);
			}
		}catch(Exception e){
			logger.error("saveMallCategory-error", e);
		}
		
		
		return vo.getId();
	}
	
	public MallCategoryVo findMallCategoryById(long id){
	  MallCategoryVo search = new MallCategoryVo();
	  search.setId(id);
	  List<MallCategoryVo> ret = this.findMallCategorys(search);
	  return ret.isEmpty()?null:ret.get(0);
	}
	
	public List<MallCategoryVo> findMallCategorys(MallCategoryVo vo){
		List<MallCategoryVo> finalRet = new ArrayList<MallCategoryVo>();
		try{
			List<MallCategoryEntity> ret = mallCategoryDAO.queryMallCategory(vo);
			if(ret!=null&&!ret.isEmpty()){
				for(MallCategoryEntity entity:ret){
					MallCategoryVo tmp = new MallCategoryVo();
					BeanUtils.copyProperties(entity, tmp);
					finalRet.add(tmp);
				}
			}
		}catch(Exception e){
			logger.error("findMallCategorys-error", e);
		}
		return finalRet;
	}
	
	public List<MallCategoryVo> findMallCategorysCheck(MallCategoryVo vo){
		List<MallCategoryVo> finalRet = new ArrayList<MallCategoryVo>();
		List<MallCategoryEntity> ret = mallCategoryDAO.queryMallCategoryChecked(vo);
		if(ret!=null&&!ret.isEmpty()){
			for(MallCategoryEntity entity:ret){
				MallCategoryVo tmp = new MallCategoryVo();
				BeanUtils.copyProperties(entity, tmp);
				finalRet.add(tmp);
			}
		}
		return finalRet;
	}
	
	public int findMaxSorNoByParentId(Long parentId){
		Long maxSort = mallCategoryDAO.findMaxSortNo(parentId);
		return maxSort==null?0:maxSort.intValue();
	}
	
	public String findName(long categoryId) throws Exception{
		return this.mallCategoryDAO.findName(categoryId);
	}
	
}
